<html>
<head>
  <title>GIT分支管理是一门艺术</title>
  <basefont face="微软雅黑" size="2" />
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  <meta name="exporter-version" content="Evernote Windows/303788 (zh-CN, DDL); Windows/6.1.7601 Service Pack 1 (Win64);"/>
  <style>
    body, td {
      font-family: 微软雅黑;
      font-size: 10pt;
    }
  </style>
</head>
<body>
<a name="475"/>
<h1>GIT分支管理是一门艺术</h1>

<div>
<span><div><h1 style="margin: 0px auto; padding: 10px 0px; width: 650px; text-align: center; border-bottom-style: dotted; border-bottom-width: 1px; border-bottom-color: rgb(178, 178, 178); font-size: 20px; color: rgb(35, 35, 35); font-family: verdana, Arial, Helvetica, sans-serif; font-style: normal; font-variant: normal; letter-spacing: normal; orphans: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><a href="http://kb.cnblogs.com/page/132209/" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0); text-decoration: none; font-size: 20px;">GIT分支管理是一门艺术</a></h1><div style="margin: 5px 0px 25px; padding: 0px; text-align: center; font-size: 12px; position: relative; color: rgb(64, 64, 64); font-family: verdana, Arial, Helvetica, sans-serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background: transparent;">作者: Vincent Driessen  发布时间: 2012-02-22 11:26  阅读: 15791 次  推荐: 7   <a href="http://roclinux.cn/?p=2129" style="margin: 0px; padding: 0px; color: rgb(26, 100, 162); text-decoration: none;" target="_blank">原文链接</a>   <a style="margin: 0px; padding: 0px; color: rgb(26, 100, 162); text-decoration: none;">[收藏]</a>  </div><div style="margin: 0px; padding: 0px; width: 690px; font-size: 14px; color: rgb(35, 35, 35); font-family: verdana, Arial, Helvetica, sans-serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><div style="margin: 0px 10px 0px 15px; padding: 0px; font-size: 14px; overflow: hidden; text-align: left; font-weight: normal; color: rgb(35, 35, 35);"><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);">　　<strong style="margin: 0px; padding: 0px; font-weight: bold; color: rgb(51, 51, 51);">英文原文：</strong><a href="http://www.nvie.com/posts/a-successful-git-branching-model/" style="margin: 0px; padding: 0px; color: rgb(26, 100, 162); text-decoration: none;" target="_blank">http://www.nvie.com/posts/a-successful-git-branching-model/</a></p><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);">　　<strong style="margin: 0px; padding: 0px; font-weight: bold; color: rgb(51, 51, 51);">原文作者：</strong>Vincent Driessen</p><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);">　　<strong style="margin: 0px; padding: 0px; font-weight: bold; color: rgb(51, 51, 51);">本文经Linux大棚博主总结精简而成。</strong></p><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);">　　<strong style="margin: 0px; padding: 0px; font-weight: bold; color: rgb(51, 51, 51);">1</strong></p><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);">　　GIT，在技术层面上，绝对是一个无中心的分布式版本控制系统，但在管理层面上，我建议你保持一个中心版本库。</p><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35); text-align: center;"><a style="margin: 0px; padding: 0px;"><img src="GIT分支管理是一门艺术_files/Image.jpg" type="image/jpeg" alt="git中心版本库" height="356" style="cursor: default; height: auto;" title="centr-decentr" width="478"/></a></p><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);">　　<strong style="margin: 0px; padding: 0px; font-weight: bold; color: rgb(51, 51, 51);">2</strong></p><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);">　　我建议，一个中心版本库(我们叫它origin)至少包括两个分支，即“主分支(master)”和“开发分支(develop)”</p><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35); text-align: center;"><a style="margin: 0px; padding: 0px;"><img src="GIT分支管理是一门艺术_files/Image [1].jpg" type="image/jpeg" alt="bigpicture-git-branch-all" height="378" style="cursor: default; height: auto;" title="git-master-develop" width="254"/></a></p><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);">　　<strong style="margin: 0px; padding: 0px; font-weight: bold; color: rgb(51, 51, 51);">3</strong></p><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);">　　要确保：团队成员从主分支(master)获得的都是处于可发布状态的代码，而从开发分支(develop)应该总能够获得最新开发进展的代码。</p><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);">　　<strong style="margin: 0px; padding: 0px; font-weight: bold; color: rgb(51, 51, 51);">4</strong></p><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);">　　在一个团队开发协作中，我建议，要有“辅助分支”的概念。</p><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);">　　<strong style="margin: 0px; padding: 0px; font-weight: bold; color: rgb(51, 51, 51);">5</strong></p><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);">　　“辅助分支”，大体包括如下几类：“管理功能开发”的分支、“帮助构建可发布代码”的分支、“可以便捷的修复发布版本关键BUG”的分支，等等。</p><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);">　　<strong style="margin: 0px; padding: 0px; font-weight: bold; color: rgb(51, 51, 51);">6</strong></p><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);">　　“辅助分支”的最大特点就是“生命周期十分有限”，完成使命后即可被清除。</p><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);">　　<strong style="margin: 0px; padding: 0px; font-weight: bold; color: rgb(51, 51, 51);">7</strong></p><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);">　　我建议至少还应设置三类“辅助分支”，我们称之为“Feature branches”，“Release branches”，“Hotfix branches”。</p><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);">　　至此，我们形成了如下这张最重要的组织组，包含了两个粗体字分支（master/develop）和三个细体字分支（feature/release/hotfixes）。</p><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35); text-align: center;"><a style="margin: 0px; padding: 0px;"><img src="GIT分支管理是一门艺术_files/Image [2].jpg" type="image/jpeg" alt="bigpicture-git-branch-all" height="815" style="cursor: default; height: auto;" title="bigpicture-git-branch-all" width="611"/></a></p><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);">　　<strong style="margin: 0px; padding: 0px; font-weight: bold; color: rgb(51, 51, 51);">8</strong></p><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);">　　“Feature branches”，起源于develop分支，最终也会归于develop分支。</p><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);">　　<strong style="margin: 0px; padding: 0px; font-weight: bold; color: rgb(51, 51, 51);">9</strong></p><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);">　　“Feature branches”常用于开发一个独立的新功能，且其最终的结局必然只有两个，其一是合并入“develop”分支，其二是被抛弃。最典型的“Fearture branches”一定是存在于团队开发者那里，而不应该是“中心版本库”中。</p><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);">　　<strong style="margin: 0px; padding: 0px; font-weight: bold; color: rgb(51, 51, 51);">10</strong></p><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);">　　“Feature branches”起源于“develop”分支，实现方法是：</p><blockquote style="margin: 10px 0px 10px 25px; padding: 5px 10px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(51, 51, 51); border: 2px solid rgb(239, 239, 239); background: none;">git checkout -b myfeature develop</blockquote><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);">　　<strong style="margin: 0px; padding: 0px; font-weight: bold; color: rgb(51, 51, 51);">11</strong></p><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);">　　“Feature branches”最终也归于“develop”分支，实现方式是：</p><blockquote style="margin: 10px 0px 10px 25px; padding: 5px 10px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(51, 51, 51); border: 2px solid rgb(239, 239, 239); background: none;">git checkout devleopgit merge --no-ff myfeature（--no-ff，即not fast forward，其作用是：要求git merge即使在fast forward条件下也要产生一个新的merge commit）（此处，要求采用--no-ff的方式进行分支合并，其目的在于，希望保持原有“Feature branches”整个提交链的完整性）git branch -d myfeaturegit push origin develop</blockquote><div style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35); text-align: center;"><div><br/></div></div><div style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);"><div><img src="GIT分支管理是一门艺术_files/Image [3].jpg" type="image/jpeg" style="cursor: default;"/>　　<strong style="margin: 0px; padding: 0px; font-weight: bold; color: rgb(51, 51, 51);">12</strong></div></div><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);">　　“Release branch”，起源于develop分支，最终归于“develop”或“master”分支。这类分支建议命名为“release-*”</p><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);">　　<strong style="margin: 0px; padding: 0px; font-weight: bold; color: rgb(51, 51, 51);">13</strong></p><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);">　　“Relase branch”通常负责“短期的发布前准备工作”、“小bug的修复工作”、“版本号等元信息的准备工作”。与此同时，“develop”分支又可以承接下一个新功能的开发工作了。</p><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);">　　<strong style="margin: 0px; padding: 0px; font-weight: bold; color: rgb(51, 51, 51);">14</strong></p><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);">　　“Release branch”产生新提交的最好时机是“develop”分支已经基本到达预期的状态，至少希望新功能已经完全从“Feature branches”合并到“develop”分支了。</p><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);">　　<strong style="margin: 0px; padding: 0px; font-weight: bold; color: rgb(51, 51, 51);">15</strong></p><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);">　　创建“Release branches”，方法是：</p><blockquote style="margin: 10px 0px 10px 25px; padding: 5px 10px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(51, 51, 51); border: 2px solid rgb(239, 239, 239); background: none;">git checkout -b release-1.2 develop./bump-version.sh 1.2 （这个脚本用于将代码所有涉及版本信息的地方都统一修改到1.2，另外，需要用户根据自己的项目去编写适合的bump-version.sh）git commit -a -m &quot;Bumped version number to 1.2&quot;</blockquote><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);">　　<strong style="margin: 0px; padding: 0px; font-weight: bold; color: rgb(51, 51, 51);">16</strong></p><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);">　　在一段短时间内，在“Release branches”上，我们可以继续修复bug。在此阶段，严禁新功能的并入，新功能应该是被合并到“develop”分支的。</p><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);">　　<strong style="margin: 0px; padding: 0px; font-weight: bold; color: rgb(51, 51, 51);">17</strong></p><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);">　　经过若干bug修复后，“Release branches”上的代码已经达到可发布状态，此时，需要完成三个动作：第一是将“Release branches”合并到“master”分支，第二是一定要为master上的这个新提交打TAG（记录里程碑），第三是要将“Release branches”合并回“develop”分支。</p><blockquote style="margin: 10px 0px 10px 25px; padding: 5px 10px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(51, 51, 51); border: 2px solid rgb(239, 239, 239); background: none;">git checkout mastergit merge --no-ff release-1.2git tag -a 1.2 （使用-u/-s/-a参数会创建tag对象，而非软tag）git checkout developgit merge --no-ff release-1.2git branch -d release-1.2</blockquote><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);">　　<strong style="margin: 0px; padding: 0px; font-weight: bold; color: rgb(51, 51, 51);">18</strong></p><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);">　　“Hotfix branches”源于“master”，归于“develop”或“master”，通常命名为“hotfix-*”</p><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);">　　<strong style="margin: 0px; padding: 0px; font-weight: bold; color: rgb(51, 51, 51);">19</strong></p><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);">　　“Hotfix branches”类似于“Release branch”，但产生此分支总是非预期的关键BUG。</p><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);">　　<strong style="margin: 0px; padding: 0px; font-weight: bold; color: rgb(51, 51, 51);">20</strong></p><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);">　　建议设立“Hotfix branches”的原因是：希望避免“develop分支”新功能的开发必须为BUG修复让路的情况。</p><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35); text-align: center;"><a style="margin: 0px; padding: 0px;"><img src="GIT分支管理是一门艺术_files/Image [4].jpg" type="image/jpeg" alt="hotfix-branches1" height="422" style="cursor: default; height: auto;" title="hotfix-branches1" width="307"/></a></p><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);">　　<strong style="margin: 0px; padding: 0px; font-weight: bold; color: rgb(51, 51, 51);">21</strong></p><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);">　　建立“Hotfix branches”，方法是：</p><blockquote style="margin: 10px 0px 10px 25px; padding: 5px 10px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(51, 51, 51); border: 2px solid rgb(239, 239, 239); background: none;">git checkout -b hotfix-1.2.1 master./bump-version.sh 1.2.1git commit -a -m &quot;Bumpt version to 1.2.1&quot; （然后可以开始问题修复工作）git commit -m &quot;Fixed severe production problem&quot; （在问题修复后，进行第二次提交）</blockquote><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);">　　<strong style="margin: 0px; padding: 0px; font-weight: bold; color: rgb(51, 51, 51);">22</strong></p><p style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);">　　BUG修复后，需要将“Hotfix branches”合并回“master”分支，同时也需要合并回“develop”分支，方法是：</p><div style="margin-top: 10px; margin-right: 0px; margin-bottom: 10px; padding: 5px 10px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(51, 51, 51); border: 2px solid rgb(239, 239, 239); background-image: none; background-position: initial initial; background-repeat: initial initial;margin-left:40px;"><div>git checkout mastergit merge --no-ff hotfix-1.2.1git tag -a 1.2.1git checkout developgit merge --no-ff hotfix-1.2.1git branch -d hotfix-1.2.1</div></div><div style="margin: 5px 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: rgb(35, 35, 35);"><div>　　</div></div></div></div></div></span>
</div></body></html> 